package com.wk.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wk.common.bean.PageVo;
import com.wk.common.bean.Query;
import com.wk.common.bean.QueryCondition;
import com.wk.pms.entity.Attr;
import com.wk.pms.entity.AttrAttrgroupRelation;
import com.wk.pms.mapper.AttrAttrgroupRelationMapper;
import com.wk.pms.mapper.AttrMapper;
import com.wk.pms.service.AttrService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wk.pms.vo.AttrVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AttrServiceImpl extends ServiceImpl<AttrMapper, Attr> implements AttrService {

    @Autowired
    private AttrAttrgroupRelationMapper relationMapper;

    @Override
    public PageVo queryByCategoryId(QueryCondition condition, Integer type, Long categoryId) {
        IPage<Attr> page = baseMapper.selectPage(
                new Query<Attr>().getPage(condition),
                new QueryWrapper<Attr>()
                        .eq(type != null, "attr_type", type)
                        .eq(categoryId != null, "catelog_id", categoryId));
        return new PageVo(page);
    }

    @Override
    public void saveAttr(AttrVO attrVo) {
        baseMapper.insert(attrVo);

        //保存数据到中间表
        AttrAttrgroupRelation relation = new AttrAttrgroupRelation();
        relation.setAttrId(attrVo.getAttrId());
        relation.setAttrGroupId(attrVo.getAttrGroupId());
        relationMapper.insert(relation);
    }
}
